//
//  CPWelcomePageViewController.m
//  Carpool_ios
//
//  Created by Wendale on 14-11-3.
//  Copyright (c) 2014年 edaijia-inc. All rights reserved.
//

#import "ICWelcomePageViewController.h"
#import "UIButton+Addition.h"
#import "MyPageControl.h"
#import "UIImage+Addition.h"

#define kPageCount 3

@interface ICWelcomePageViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)MyPageControl *pageControl;
@property(nonatomic, strong)UIButton *goBtn;
@end

@implementation ICWelcomePageViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"欢迎页面";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBCOLOR(0x2a3657);
    [self setRightBarItem];
    [self.view addSubview:self.scrollView];
    self.pageControl.bottom = kScreenHeight  - 90.f;
    self.pageControl.centerX = kScreenWidth/2.f;
    [self.view addSubview:self.pageControl];
    
//    NSString *lastStr = @"";
//    if ([self isIPhone6p]) {
//        lastStr = @"6p";
//    }else if([self isIPhone6]){
//        lastStr = @"6";
//    }else if([self isIPhone5]){
//        lastStr = @"5";
//    }else if([self isIPhone4]){
//        lastStr = @"4";
//    }
    NSString *lastStr = @"";
    if ([self isIPhone6p]) {
        lastStr = @"6p";
    }else if([self isIPhone6]){
        lastStr = @"6s";
    }else if([self isIPhone5]){
        lastStr = @"5s";
    }else if([self isIPhone4]){
        lastStr = @"4s";
    }
    //第一页
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    NSString *imgsStr1 = [NSString stringWithFormat:@"introduce%@1",lastStr];
    firstImageView.image = IMG(imgsStr1);
    firstImageView.centerX = kScreenWidth/2.f;
    [self.scrollView addSubview:firstImageView];
    
    //第二页
    UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    NSString *imgsStr2 = [NSString stringWithFormat:@"introduce%@2",lastStr];
    secondImageView.image = IMG(imgsStr2);
    secondImageView.centerX = kScreenWidth + kScreenWidth/2.f;
    [self.scrollView addSubview:secondImageView];
    
    //第三页
    UIImageView *thirdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    NSString *imgsStr3 = [NSString stringWithFormat:@"introduce%@3",lastStr];
    thirdImageView.image = IMG(imgsStr3);
    thirdImageView.centerX = kScreenWidth * 2 + kScreenWidth/2.f;
    [self.scrollView addSubview:thirdImageView];

    
//    UIImageView *fourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    NSString *imgsStr4 = [NSString stringWithFormat:@"引导页4%@",lastStr];
//    fourImageView.image = IMG(imgsStr4);
//    fourImageView.centerX = kScreenWidth * 3 + kScreenWidth/2.f;
//    [self.scrollView addSubview:fourImageView];
    
    self.goBtn.bottom = kScreenHeight - 30.f;
    [self.scrollView addSubview:self.goBtn];
 //   self.loginBtn.bottom = kScreenHeight;
//    [self.view addSubview:self.loginBtn];
//    self.registerBtn.bottom = kScreenHeight;
//    [self.view addSubview:self.registerBtn];
}

- (BOOL)isIPhone4{
    return kScreenHeight == 480 ? YES : NO;
}

- (BOOL)isIPhone5{
    return kScreenHeight == 568 ? YES : NO;
}

- (BOOL)isIPhone6{
    return kScreenHeight == 667 ? YES : NO;
}

- (BOOL)isIPhone6p{
    return kScreenHeight >= 736 ? YES : NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setRightBarItem
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"跳过" forState:UIControlStateNormal];
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton setTitleColor:RGBCOLOR(0x2f97d6) forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [rightButton addTarget:self action:@selector(onClickRightBtn)
          forControlEvents:UIControlEventTouchUpInside];
    [rightButton sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)onClickRightBtn{
    [APP_CONTEXT.accountManager setWelcomeShowed];
    [UIView animateWithDuration:0.3f animations:^{
        self.view.alpha = 0.5;
    } completion:^(BOOL finished) {
        [ICNavigator openRootVC];
    }];
}

- (void)onClickGoBtn{
    [self onClickRightBtn];
}

- (UIScrollView*)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.height)];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(kScreenWidth*kPageCount, self.view.height);
        _scrollView.showsHorizontalScrollIndicator=NO;
    }
    return _scrollView;
}

- (MyPageControl*)pageControl{
    if (!_pageControl) {
        CGFloat dotGapWidth = 8.f;
        CGFloat pageControlWidth = kPageCount * IMG(@"intropage_dotgrey").size.width + (kPageCount - 1) * dotGapWidth;
        _pageControl = [[MyPageControl alloc] initWithFrame:CGRectMake(0, 0, pageControlWidth, dotGapWidth)
                                                normalImage:IMG(@"intropage_dotgrey")
                                           highlightedImage:IMG(@"intropage_dotgreeen")
                                                 dotsNumber:kPageCount
                                                 sideLength:dotGapWidth
                                                    dotsGap:dotGapWidth];
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}
#pragma mark - scrollView
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offset=scrollView.contentOffset;
    CGRect bounds=scrollView.frame;
    [self.pageControl setCurrentPage:offset.x/bounds.size.width animated:YES];
}

- (UIButton *)goBtn{
    if (!_goBtn) {
        _goBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth*(kPageCount -1) + kScreenWidth/2.f - 100.f, 0, 210, 60.f)];
        [_goBtn setBackgroundColor:[UIColor clearColor]];
        [_goBtn addTarget:self action:@selector(onClickGoBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goBtn;
}


@end
